import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xiaomi_shop/app/api/api.dart';
import 'package:xiaomi_shop/app/services/userService.dart';
import 'package:xiaomi_shop/app/services/xhttp.dart';
import 'package:xiaomi_shop/models/messageModel.dart';

class PhoneAccountLoginController extends GetxController {
  final TextEditingController phoneEditController = TextEditingController();
  final TextEditingController passwordEditController = TextEditingController();

  var agreePrivacy = false.obs;
  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  // 登录
  Future<MessageModel> login() async {
    var response = await XHttp.getInstance().post(Api.doLogin, {
      "username": phoneEditController.text,
      "password": passwordEditController.text
    });
    if (response != null) {
      Result result = response as Result;
      if (result.data["success"]) {
        List userinfos = result.data["userinfo"];
        UserService.saveUser(userinfos.first);
        return MessageModel(success: true, message: "登录成功");
      } else {
        return MessageModel(success: false, message: result.data["message"]);
      }
    } else {
      return MessageModel(success: false, message: "网络错误");
    }
  }
}
